#include "LuaLink_Entity.h"

LuaLink_Entity::LuaLink_Entity(const char* name, const char* mesh) {
	mEntity = LuaLink_SceneManager::INSTANCE->mSceneManager->createEntity(name, mesh);
}

LuaLink_Entity::~LuaLink_Entity(void) {
}

unsigned int LuaLink_Entity::lua_getAnimationCount() {
	unsigned int total = 0;
	Ogre::AnimationStateIterator iter = mEntity->getAllAnimationStates()->getAnimationStateIterator();
	while (iter.hasMoreElements()) {
		iter.getNext();
		total++;
	}
	return total;
}

const char* LuaLink_Entity::lua_getAnimationName(unsigned int idx) {
	unsigned int total = 0;
	Ogre::AnimationStateIterator iter = mEntity->getAllAnimationStates()->getAnimationStateIterator();
	while (iter.hasMoreElements()) {
		if (idx == total) {
			Ogre::AnimationState* as = iter.getNext();
			return as->getAnimationName().c_str();
		}
		iter.getNext();
		total++;
	}
	return "";
}

void LuaLink_Entity::lua_animation(const char* name, bool enabled, bool loop) {
	Ogre::AnimationState* as = mEntity->getAnimationState(name);
	if (as != NULL) {
		as->setEnabled(enabled);
		as->setLoop(loop);
	}
}

void LuaLink_Entity::lua_animate(const char* name, Ogre::Real amount) {
	Ogre::AnimationState* as = mEntity->getAnimationState(name);
	if (as != NULL) {
		as->addTime(amount);
	}
}

float LuaLink_Entity::lua_getAnimationLength(const char* name) {
	Ogre::AnimationState* as = mEntity->getAnimationState(name);
	if (as != NULL) {
		return as->getLength();
	}

	return 0;
}

void LuaLink_Entity::lua_setAnimationTime(const char* name, float time) {
	Ogre::AnimationState* as = mEntity->getAnimationState(name);
	if (as != NULL) {
		as->setTimePosition(time);
	}
}

float LuaLink_Entity::lua_getAnimationTime(const char* name) {
	Ogre::AnimationState* as = mEntity->getAnimationState(name);
	if (as != NULL) {
		return as->getTimePosition();
	}

	return 0;
}
